package dkbta.ontology.elements.pattern.conditions;

import dkbta.ontology.elements.Element;
import dkbta.ontology.values.DurationValues;
import dkbta.util.time.TimeInterval;

/**
 * A temporal condition that checks whether two elements are overlapping, the duration of
 * the overlap and the distance between the start times of the two elements
 * 
 * @author Uri Kanonov
 */
public class OverlappingTemporalCondition extends TemporalCondition{
	/** Valid durations for the overlap */
	private final DurationValues _validOverlapDuration;

	/** Valid durations for the distance between the start times of the two elements */
	private final DurationValues _validStartingDistance;

	/**
	 * Initializes an overlapping temporal condition
	 * 
	 * @param validOverlapDuration Valid durations for the overlap
	 * @param validStartingDistance Valid durations for the distance between the start times of the two elements
	 */
	public OverlappingTemporalCondition(DurationValues validOverlapDuration,
		DurationValues validStartingDistance){
		_validOverlapDuration = validOverlapDuration;
		_validStartingDistance = validStartingDistance;
	}

	@Override
	public boolean checkCondition(Element first, Element second){
		TimeInterval secondTI = second.getTimeInterval();
		TimeInterval firstTI = first.getTimeInterval();
		TimeInterval overlap = secondTI.getOverlap(firstTI);
		if (overlap == null){
			return false;
		}
		long startingDistance = secondTI.getStartTime() - firstTI.getStartTime();
		return _validOverlapDuration.isValid(overlap.getDuration())
				&& _validStartingDistance.isValid(startingDistance);
	}

	@Override
	public String toString(){
		return "Overlapping - Overlap Values: " + _validOverlapDuration
				+ ", Starting Distance Values: " + _validStartingDistance;
	}
}
